<?php
declare (strict_types=1);

namespace app\common\tool;

use think\facade\Log;

class KdApi
{
    const EBusinessID = '1237100';//即用户ID，登录快递鸟官网会员中心获取 https://www.kdniao.com/UserCenter/v4/UserHome.aspx

    const ApiKey = '56da2cf8-c8a2-44b2-b6fa-476cd7d1ba17';//即API key，登录快递鸟官网会员中心获取 https://www.kdniao.com/UserCenter/

    const ReqURL = 'https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';//

    /**
     * 物流查询
     */
    public function getOrderTracesByJson($requestData)
    {
        // 组装应用级参数
        /*$requestData= "{".
            "'OrderCode': '',".
            "'CustomerName': '',".
            "'LogisticCode': '773026589419357',".
            "}";*/
        // 组装系统级参数
        $datas = array(
            'EBusinessID' => self::EBusinessID,
            'RequestType' => '8002', //快递查询接口指令8002/地图版快递查询接口指令8004
            'RequestData' => urlencode($requestData) ,
            'DataType' => '2',
        );
        $datas['DataSign'] = $this->encrypt($requestData, self::ApiKey);
        //以form表单形式提交post请求，post请求体中包含了应用级参数和系统级参数
        $result = $this->sendPost(self::ReqURL, $datas);

        //根据公司业务处理返回的信息......

        //没有物流轨迹的
        /*$result = '{
            "LogisticCode":"7720046515948751",
            "Traces":[

            ],
            "State":"0",
            "EBusinessID":"1470400",
            "Reason":"暂无轨迹信息",
            "Success":true
        }';*/

        //有物流轨迹的
        $result = '{
            "StateEx":"2",
            "LogisticCode":"3103630045838",
            "ShipperCode":"YD",
            "Traces":[
                {
                    "Action":"1",
                    "AcceptStation":"【深圳市】广东深圳公司中心分拨分部 已揽收",
                    "AcceptTime":"2020-06-17 03:31:04",
                    "Location":"深圳市"
                },
                {
                    "Action":"2",
                    "AcceptStation":"【深圳市】已离开 广东深圳公司中心分拨分部；发往 上海分拨中心",
                    "AcceptTime":"2020-06-17 03:34:08",
                    "Location":"深圳市"
                },
                {
                    "Action":"2",
                    "AcceptStation":"【深圳市】广东深圳公司 已揽收",
                    "AcceptTime":"2020-06-17 03:37:46",
                    "Location":"深圳市"
                },
                {
                    "Action":"2",
                    "AcceptStation":"【深圳市】已到达 广东深圳公司",
                    "AcceptTime":"2020-06-17 08:02:33",
                    "Location":"深圳市"
                },
                {
                    "Action":"2",
                    "AcceptStation":"【深圳市】已离开 广东深圳公司；发往 上海分拨中心",
                    "AcceptTime":"2020-06-17 08:11:33",
                    "Location":"深圳市"
                },
                {
                    "Action":"2",
                    "AcceptStation":"【深圳市】广东深圳公司福田区水围站分部 已揽收",
                    "AcceptTime":"2020-06-17 09:22:25",
                    "Location":"深圳市"
                }
            ],
            "State":"2",
            "EBusinessID":"1470400",
            "Success":true,
            "Location":"深圳市"
        }';

        return $result;
    }

    /**
     * post提交数据
     * @param  string $url 请求Url
     * @param  array $datas 提交的数据
     * @return url响应返回的html|false|string
     */
    private function sendPost($url, $datas)
    {
        $postdata = http_build_query($datas);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间（单位:s）
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }

    /**
     * 电商Sign签名生成
     * @param data 内容
     * @param ApiKey ApiKey
     * @return string
     */
    private function encrypt($data, $ApiKey)
    {
        return urlencode(base64_encode(md5($data.$ApiKey)));
    }


}